﻿/* You are given an array of strings. Write a method that sorts the array by the length of its elements (the number of characters composing them). */


using System;

class SortStringLength
{
    static void Main()
    {
        uint N;

        bool iProvided = false;
        do
        {
            if (iProvided == true) Console.WriteLine("Error: invalid input! Please enter a positive integer.\r\n\t* Valid input: N > 0");
            else iProvided = true;
            Console.Write("Enter N: ");
        } while (UInt32.TryParse(Console.ReadLine(), out N) == false || N < 1);

        string[] nums = new string[N];
        for (int i = 0; i < nums.Length; i++)
        {
            Console.WriteLine();
            Console.Write("Enter array element #{0} \r\n(Valind input: everything, \r\nwill be casted as a string): ", i+1);
            nums[i] = Console.ReadLine();
        }

        string[] sortarr = (string[])nums.Clone();
        SortByStringLength(sortarr);

        //print array (sorted)
        foreach (string str in sortarr) Console.WriteLine(str);
    }

    //method to sort strings by string length using the bubble sort algorithm
    private static void SortByStringLength(string[] arr)
    {
        string stack = arr[0];
        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = i + 1; j < arr.Length; j++)
            {

                if (arr[i].Length > arr[j].Length)
                {
                    stack = arr[i];
                    arr[i] = arr[j];
                    arr[j] = stack;
                }
            }

        }

    }
}
